package com.hsy.crm.web.service.impl;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Product;
import com.hsy.crm.web.domain.entity.Unit;
import com.hsy.crm.web.domain.po.unit.UnitPagePo;
import com.hsy.crm.web.domain.vo.unit.UnitPageVo;
import com.hsy.crm.web.mapper.ProductMapper;
import com.hsy.crm.web.mapper.UnitMapper;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.utils.ListUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Product;
import com.hsy.crm.web.domain.entity.Unit;
import com.hsy.crm.web.domain.po.unit.UnitPagePo;
import com.hsy.crm.web.domain.vo.unit.UnitPageVo;
import com.hsy.crm.web.mapper.ProductMapper;
import com.hsy.crm.web.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UnitService extends BaseService<Unit> {

    @Autowired
    UnitMapper unitMapper;

    @Autowired
    ProductMapper productMapper;

    public Unified<Integer> deleteUnit(Unit unit) {
        Unified<Integer> unified = new Unified<>();
        Product product = new Product();
        product.setUnitId(unit.getId());
        product.setCustomerId(unit.getCustomerId());
        List<Product> products = productMapper.select(product);
        if(ListUtils.isEmpty(products)){
            this.delete(unit);
        }else{
            unified.setResult(App.BussError.UNIT_DELETE_PRODUCT.getCode());
        }
        return unified;
    }

    public PageResult<UnitPagePo> page(UnitPageVo unitPageVo) {
        unitPageVo.setType(App.UnitType.MASTER);
        Page page = PageHelper.startPage(unitPageVo.getPage(),unitPageVo.getLimit());
        PageResult<UnitPagePo> pageResult = new PageResult<>();
        pageResult.setRows(unitMapper.page(unitPageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

}
